package cn.niit.letuseatdemo.service.impl;

import cn.niit.letuseatdemo.entity.domain.Goods;
import cn.niit.letuseatdemo.entity.domain.Store;
import cn.niit.letuseatdemo.mapper.GoodsMapper;
import cn.niit.letuseatdemo.service.GoodsService;
import cn.niit.letuseatdemo.util.LoginStoreUtil;
import cn.niit.letuseatdemo.util.RedisUtil;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


/**
 * 用户Service业务层处理
 *
 * @author su_fuming
 * @date 2022-10-15
 */
@Service
@Slf4j
public class GoodsServiceImpl implements GoodsService
{
    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private GoodsMapper goodsMapper;



    /**
     * 查询用户
     *
     * @param goodsId 用户主键
     * @return 用户
     */
    @Override
    public Goods selectGoodsByGoodsId(Long goodsId)
    {
        return goodsMapper.selectGoodsByGoodsId(goodsId);
    }

    /**
     * 查询用户列表
     *
     * @param goods 用户
     * @return 用户
     */
    @Override
    public List<Goods> selectGoodsList(Goods goods){
    /**
     *  开启分页查询
     *  PageHelper.startPage(goods.getPage(),goods.getRows())
     *  1.将改代码放在要执行分页查询语句之前
     *   2.两个参数，分别是当前第几页，每页记录数
     **/
        if(goods.getPage() == null){
            goods.setPage(1);
        }
        PageHelper.startPage(goods.getPage(),goods.getRows());
    List<Goods> goodsList = goodsMapper.selectGoodsList(goods);
        return goodsList;
    }


    /*
    * @Description: 查询菜品列表（vue前端用）
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/7 18:28
     * @param: goods
     * @return: java.util.List<cn.niit.letuseatdemo.entity.domain.Goods>
    **/
    @Override
    public List<Goods> getAllGoodsList(Goods goods) {
        return goodsMapper.selectGoodsList(goods);
    }

    /*
     * @Description:根据商家id获取菜品信息
     * @Author: Zhang Shiqi
     * @CreateTime： 2022/12/8 4:33
     * @param: storeId
     * @return: cn.niit.letuseatdemo.entity.domain.Goods
     **/
    @Override
    public List<Goods> selectGoodsByStoreId(Goods goods, Long storeId) {
        return  goodsMapper.selectGoodsByStoreId(storeId);
    }



    /*
    * @Description: 详细菜品显示
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/8 4:46
     * @param: storeId
     * @param: goodsId
     * @return: cn.niit.letuseatdemo.entity.domain.Goods
    **/
    @Override
    public Goods selectGoodsByStoreIdAndGoodsId(Long storeId, Long goodsId) {
        return goodsMapper.selectGoodsByStoreIdAndGoodsId(storeId,goodsId);
    }

    /*
    * @Description: 查菜品列表里的菜品类别
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/8 10:25
     * @param: goods
     * @param: goodsId
     * @return: java.util.List<cn.niit.letuseatdemo.entity.domain.Goods>
    **/
    @Override
    public List<Goods> selectGoodsClassifyByGoodsId(Goods goods,Long storeId, Long goodsId) {
        return null;
    }

    /*
    * @Description: 根据菜品类别查菜品列表
    * @Author: Zhang Shiqi
    * @CreateTime： 2022/12/8 10:39
     * @param: goods
     * @param: storeId
     * @param: goodsClassify
     * @return: java.util.List<cn.niit.letuseatdemo.entity.domain.Goods>
    **/
    @Override
    public List<Goods> selectGoodsByGoodsClassify(Goods goods, Long storeId, String goodsClassify) {
        return goodsMapper.selectGoodsByGoodsClassify(storeId,goodsClassify);
    }


    /**
     * 新增用户
     *
     * @param goods 用户
     * @return 结果
     */
    @Override
    public int insertGoods(Goods goods)
    {
        /** 为delFlag 赋予默认值*/
        goods.setDelFlag(0);
        /** 获取用户信息，为department 赋值，创建人信息（id,姓名)，创建时间 */
        Store loginStore = LoginStoreUtil.getLoginStore();
        goods.setCreateUserId(loginStore.getStoreId());
        goods.setCreateUserName(loginStore.getStoreName());
        goods.setGmtCreate(new Date());
        return goodsMapper.insertGoods(goods);
    }

    /**
     * 修改用户
     *
     * @param goods 用户
     * @return 结果
     */
    @Override
    public int updateGoods(Goods goods)
    {
        Store loginStore = LoginStoreUtil.getLoginStore();
        goods.setUpdateUserId(loginStore.getStoreId());
        goods.setUpdateUserName(loginStore.getStoreName());
        goods.setGmtUpdate(new Date());
        return goodsMapper.updateGoods(goods);
    }
    /**
     * 批量删除用户
     *
     * @param goodsIds 需要删除的用户主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByGoodsIds(String goodsIds)
    {
        return 0;
    }
    /**
     * 删除用户信息
     *
     * @param goodsId 用户主键
     * @return 结果
     */
    @Override
    public int deleteGoodsByGoodsId(Long goodsId)
    {
        return goodsMapper.deleteGoodsByGoodsId(goodsId);
    }

    /**
     * 修改菜品状态
     *
     * @param goods 用户
     * @return 结果
     */
    @Override
    public int updateGoodsStatus(Goods goods) {
        return goodsMapper.updateGoodsStatus(goods);
    }
}
